package com.basic.library.utils.audio;

import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Handler;

import androidx.fragment.app.FragmentActivity;

import com.basic.library.Interface.MCallback;
import com.basic.library.utils.PermissionUtil;
import com.basic.library.utils.toast.ToastCompat;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * @author yj
 * @description
 */
public class ChatUtil {
    private static ChatUtil chatUtil = new ChatUtil();

    public static ChatUtil getInstance() {
        return chatUtil;
    }


    Disposable disposable;
    Handler mHandler;
    long timeLong = 0, duration = 60;

    /**
     * 录音事件
     *
     * @param context
     * @param mHandler
     */
    public void audioSend(FragmentActivity context, Handler mHandler) {
        if (AudioManager.getInstance().generalFileName() == null) {
            ToastCompat.show("未进入车队界面");
            return;
        }
        this.mHandler = mHandler;
        MAudioManager.getInstance().initAudioManager(context);
        PermissionUtil.authorOnceWithGranted(context, new MCallback<Boolean>() {
                    @SuppressLint("CheckResult")
                    @Override
                    public void onResponse(Boolean aBoolean) {
                        if (aBoolean) {
                            if (AudioManager.getInstance().isPrepared()) {
                                if (disposable != null && !disposable.isDisposed()) {
                                    disposable.dispose();
                                    disposable = null;
                                    mHandler.sendEmptyMessage(-1);

                                }
                            } else {
                                AudioManager.getInstance()
                                        .setOnAudioStageListener(() -> {
                                            disposable = Observable.intervalRange(0, duration + 1, 0, 1, TimeUnit.SECONDS)
                                                    .subscribeOn(AndroidSchedulers.mainThread())
                                                    .observeOn(AndroidSchedulers.mainThread())
                                                    .subscribe(new Consumer<Long>() {
                                                        @Override
                                                        public void accept(Long aLong) throws Exception {
                                                            timeLong = aLong;
                                                            if (aLong == duration) {
                                                                mHandler.sendEmptyMessage(-1);

                                                            } else {
                                                                mHandler.sendEmptyMessage(aLong.intValue());
                                                            }
                                                        }
                                                    });
                                        })
                                        .prepareAudio();
                            }
                        } else {
                            ToastCompat.show("请到设置-权限中打开语音权限");
                        }
                    }
                },
                Manifest.permission.RECORD_AUDIO);         //语音
    }

}
